using System;
using System.Runtime.InteropServices;

namespace WiMo.Games.Inputs
{
    public class KeyPressInfo
    {
        public static DateTime AbsoluteStart = DateTime.Now;
        public bool FromAccelerometer { get; set; }

#if(SILVERLIGHT)
        public uint GetTickCount()
        {
            return (uint)new TimeSpan(DateTime.Now.Ticks).TotalMilliseconds;
        }
#else
#if(!ZUNE)
        [DllImport("coredll.dll")]
		        public static extern uint GetTickCount();
#else
        public uint GetTickCount()
        {
            return (uint)(DateTime.Now - KeyPressInfo.AbsoluteStart).TotalMilliseconds;
        }
#endif
#endif

        public KeyPressInfo(Keys key)
            : this(key, false)
        {

        }

        public KeyPressInfo(Keys key, bool fromAccelerometer)
        {
            FromAccelerometer = fromAccelerometer;
            Key = key;
            //Set to zero so it trips the first time through.
            Start = 0;
        }

        public KeyPressInfo(Keys key, uint start)
        {
            Key = key;
            Start = start;
        }

        public KeyPressInfo Clone()
        {
            var kpi = new KeyPressInfo(Key, Start);

            return kpi;
        }

        public Keys Key { get; private set; }
        public uint Start { get; set; }
        public bool Debounce(int ms)
        {
            uint currentTicks = GetTickCount();
            uint elapsedTickets = (currentTicks - Start);
            var elapsedMs = elapsedTickets;// / 1000;// TimeSpan.TicksPerMillisecond;            

            if (elapsedMs < ms)
                return false;

            Start = GetTickCount();

            return true;
        }
    }
}
